package setup

import (
	"context"
	"fmt"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"log"

	"github.com/minio/minio-go/v7"

	"my-gin-tiktok/global"
)

func InitMinioClient() *minio.Client {
	minioClient, err := minio.New(
		global.App.Config.OSS.EndPoint,
		&minio.Options{
			Creds:  credentials.NewStaticV4(global.App.Config.OSS.AccessKey, global.App.Config.OSS.AccessSecret, ""),
			Secure: global.App.Config.OSS.SSL,
		})

	if err != nil {
		log.Fatalln(err)
	}

	// 检查存储桶是否已经存在
	videoBucketName := global.App.Config.OSS.VideoBucketName
	exists, errBucketExists := minioClient.BucketExists(context.Background(), videoBucketName)
	if errBucketExists == nil && exists {
		fmt.Printf("Bucket %s already exists\n", videoBucketName)
	} else {
		log.Fatalln(err)
	}

	imageBucketName := global.App.Config.OSS.ImageBucketName
	exists, errBucketExists = minioClient.BucketExists(context.Background(), imageBucketName)
	if errBucketExists == nil && exists {
		fmt.Printf("Bucket %s already exists\n", imageBucketName)
	} else {
		log.Fatalln(err)
	}

	return minioClient
}
